import {ElLoading, ElMessage} from "element-plus";
import HttpClient from "../../ark/http-client/http-client.ts";
import {MockMode} from "../../ark/http-client/http-client-type.js";
// import {PaginationData} from "@/base-components/AppTable/AppTableOptions.js";
import {AuthenticationType} from "@/constant/authentication-type.js";
import type {AxiosRequestHeaders} from "axios";

export default new HttpClient(
    {
      baseURL: import.meta.env.VITE_HTTP_CLIENT_BASE_URL,

      showLoading: false,
      showLoadingFunction() {
        ElLoading.service();
      },
      hideLoadingFunction() {
        ElLoading.service().close();
      },

      handleRequestHeaders(headers: AxiosRequestHeaders) {
        // 此处常用于添加权限验证token请求头
        // const tokenInfo = useGeneralStore()?.tokenInfo;
        // if (tokenInfo.value) {
        //     headers[tokenInfo.key] = `Bearer ${tokenInfo.value}`;
        // }
      },

      serverResponseSuccess(serverResponseBody) {
        return String(serverResponseBody.code) === AuthenticationType.SUCCESS;
      },
      authenticationFailure(serverResponseBody) {
        return [
          AuthenticationType.TOKEN_INVALID,
          AuthenticationType.TOKEN_TIMEOUT,
          AuthenticationType.TOKEN_NOT_FOUND,
        ].includes(serverResponseBody.code);
      },
      handleAuthenticationFailure() {
        // 此处常用于退出登录并跳转登录页
        // useAccountStore().logout();
      },

      showSuccessMsg: false,
      showSuccessMsgFunction(msg) {
        ElMessage.success(msg);
      },

      showFailureMsg: true,
      showFailureMsgFunction(msg) {
        ElMessage.error(msg);
      },
    },
    {
      mode: import.meta.env.VITE_HTTP_CLIENT_MOCK_MODE as MockMode,
      mixModeDefaultMocked:
          import.meta.env.VITE_HTTP_CLIENT_MIX_MODE_DEFAULT_MOCKED === "true",
      defaultMockServerBaseUrl:
      import.meta.env.VITE_HTTP_CLIENT_DEFAULT_MOCK_SERVER_BASE_URL,
    },
);

export interface ResponseBody<T> {
  data: T;
  msg: string;
  code: string;
}

// export type PaginationDataResponseBody<T> = ResponseBody<PaginationData<T>>;